package com.zf.zffencheng.strategy;

import com.zf.zffencheng.exception.ResultException;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * ClassName: DeviceFactory <br/>
 * Description:  <br/>
 * date: 2021/5/31 9:55<br/>
 *
 * @author william <br/>
 */
@Log4j2
public class DeviceFactory {

    public static final Map<Integer, DeviceStrategy> DEVICE_HANDLER_SERVICE = new ConcurrentHashMap<>();

    public static void register(Integer key, DeviceStrategy service) {
        log.info("设备加载:key:{}, class:{}", key, service.getClass().getName());
        DEVICE_HANDLER_SERVICE.put(key, service);
    }

    public static DeviceStrategy get(Integer key) {
        DeviceStrategy service = DEVICE_HANDLER_SERVICE.get(key);
        if (service == null) {
            throw new ResultException(511, "设备类型不对");
        }
        return service;
    }

    public static DeviceStrategy find(Integer key) {
        return DEVICE_HANDLER_SERVICE.get(key);
    }

}
